local extension = Package("tshu_ox")
extension.extensionName = "tianshu"

Fk:loadTranslationTable{
  ["tshu_ox"] = "虚拟偶像",
  ["tshuox"] = "虚拟偶像",
}

local U = require "packages/utility/utility"


local xuxiang = fk.CreateTriggerSkill{
  name = "tshu__xuxiang",
  anim_type = "support",
  frequency = Skill.Compulsory,
  events = {fk.DamageInflicted},
  can_trigger = function(self, event, target, player, data)
    if event == fk.DamageInflicted then
        return target == player and player:hasSkill(self) 
    end
  end,
  on_use = function(self, event, target, player, data)
      return true
  end,
}
local xuxiang_buff = fk.CreateProhibitSkill{
  name = "#tshu__xuxiang_buff",
  frequency = Skill.Compulsory,
  is_prohibited = function(self, from, to, card)
      return to:hasSkill(self) and from.role == "rebel"
  end,
}
local xuxiang_prohibit = fk.CreateProhibitSkill{
  name = "#xuxiang_prohibit",
  prohibit_use = function(self, player, card)
      return player:hasSkill(self) 
  end,
  prohibit_response = function(self, player, card)
      return player:hasSkill(self) 
  end,
}
xuxiang:addRelatedSkill(xuxiang_prohibit)
xuxiang:addRelatedSkill(xuxiang_buff)
Fk:addSkill(xuxiang)
Fk:loadTranslationTable{
  ["tshu__xuxiang"] = "虚像",
  [":tshu__xuxiang"] = "锁定技，防止你受到的伤害，你无法使用或打出牌,敌方无法指定你为目标。",
  ["#xuxiang_prohibit"] = "虚像",
}

local xiaosha = General(extension, "tshuox__xiaosha","god",4,4,General.Female)
xiaosha.hidden = true
local guisha = fk.CreateTriggerSkill{
  name = "tshu__guisha",
  anim_type = "support",
  frequency = Skill.Compulsory,
  events = {fk.CardUsing},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and (target.role == "lord" or target.role == "loyalist") and data.card.trueName == "slash" and not player:isNude()
  end,
  on_cost = function(self, event, target, player, data)
    -- 收集所有可弃置的牌（手牌和装备区）
    local cards = {}
    for _, area in ipairs({Player.Hand, Player.Equip}) do
      for _, id in ipairs(player:getCardIds(area)) do
        table.insert(cards, id)
      end
    end
    
    if #cards > 0 then
      -- 随机选择一张牌
      local index = math.random(1, #cards)
      local to_discard = {cards[index]}
      
      player.room:doIndicate(player.id, {target.id})
      self.cost_data = to_discard
      return true
    end
    return false
  end,
  on_use = function(self, event, target, player, data)
    player.room:throwCard(self.cost_data, self.name, player, player)
    if not data.extraUse then
      data.extraUse = true
      target:addCardUseHistory(data.card.trueName, -1)
    end
    data.additionalDamage = (data.additionalDamage or 0) + 1
  end,
}
local shuli = fk.CreateTriggerSkill{
  name = "tshu__shuli",
  anim_type = "support",
  events = {fk.Damage},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target and (target.role == "lord" or target.role == "loyalist") and data.card and data.card.trueName == "slash" and player:usedSkillTimes(self.name, Player.HistoryTurn) < 2
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    if not player.dead then
      player:drawCards(1,self.name)
    end
    if not target.dead then
      target:drawCards(1,self.name)
    end
  end,
}
xiaosha:addSkill(guisha)
xiaosha:addSkill(shuli)
xiaosha:addSkill("tshu__xuxiang")
xiaosha:addSkill("keji")

Fk:loadTranslationTable{
  ["tshuox__xiaosha"] = "小杀",

  ["tshu__guisha"] = "瑰杀",
  [":tshu__guisha"] = "锁定技，当一名其他角色使用【杀】时，你弃置一张牌令此【杀】伤害+1且不计入使用次数。",
  ["tshu__shuli"] = "姝丽",
  [":tshu__shuli"] = "当一名其他角色使用【杀】造成伤害后，你可以与其各摸一张牌(每回合限两次)。",
  ["#tshu__guisha-invoke"] = "瑰杀：你可以弃置一张牌令%dest的【杀】不计次数且伤害+1",

  ["$tshu__guisha"] = "瑰姿魅丽，沙场来战。",
  ["$tshu__shuli"] = "姿艳姝丽..",
  ["$tshu__xuxiang_tshuox__xiaosha"] = "可不能打女孩子哦~",
  ["~tshuox__xiaosha"] = "哼！你欺负人家~",
}

local xiaojiu = General(extension, "tshuox__xiaojiu","god",4,4,General.Female)
xiaojiu.hidden = true
local meiniang = fk.CreateTriggerSkill{
  name = "tshu__meiniang",
  anim_type = "offensive",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target.phase == Player.Play and (target.role == "lord" or target.role == "loyalist")and not target.dead
  end,
  on_cost = function(self, event, target, player, data)
    if player.room:askForSkillInvoke(player,self.name,nil) then
      player.room:doIndicate(player.id, {target.id})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if not target.dead and target:canUseTo(Fk:cloneCard("analeptic"), target) then
      room:useVirtualCard("analeptic", nil, target, target, self.name, true)
    end
  end,
}
local yaoli = fk.CreateTriggerSkill{
  name = "tshu__yaoli",
  anim_type = "offensive",
  events = {fk.CardUsing},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target.phase == Player.Play and (target.role == "lord" or target.role == "loyalist") and data.card.name == "analeptic"
  end,
  on_cost = function(self, event, target, player, data)
    if player.room:askForSkillInvoke(player,self.name,nil) then
      player.room:doIndicate(player.id, {target.id})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:addPlayerMark(target,"tshu__yaoli",1)
  end,
  refresh_events = {fk.CardUsing},
  can_refresh = function(self, event, target, player, data)
    return target == player and player:getMark("tshu__yaoli") > 0 and data.card.trueName == "slash"
  end,
  on_refresh = function(self, event, target, player, data)
    local n = player:getMark("tshu__yaoli")
    player.room:setPlayerMark(player,"tshu__yaoli",0)
    data.disresponsiveList = table.map(player.room.alive_players, Util.IdMapper)
    local targets = player.room:getUseExtraTargets( data)
    if #targets == 0 then return false end
    local tos = player.room:askForChoosePlayers(player, targets, 1, n,
    "#yaoli-choose:::"..data.card:toLogString() .. ":" .. tostring(n), self.name, true)
    if #tos > 0 then
      table.forEach(tos, function (id)
        table.insert(data.tos, {id})
      end)
    end
  end
}
xiaojiu:addSkill(meiniang)
xiaojiu:addSkill(yaoli)
xiaojiu:addSkill("tshu__xuxiang")
xiaojiu:addSkill("keji")

Fk:loadTranslationTable{
  ["tshuox__xiaojiu"] = "小酒",

  ["tshu__meiniang"] = "美酿",
  [":tshu__meiniang"] = "一名其他角色的出牌阶段开始时，你可以令其视为使用一张【酒】且不计入使用次数。",
  ["tshu__yaoli"] = "媱丽",
  [":tshu__yaoli"] = "当其他角色使用【酒】时，你可以令其使用的下一张【杀】目标数+1且无法被响应。",
  ["#yaoli-choose"] = "你可以为%arg额外指定至多%arg2个目标",

  ["$tshu__meiniang"] = "弶醇沁起，酒香馥郁",
  ["$tshu__yaoli"] = "诗酒临江，慷慨而璇。",
  ["$tshu__xuxiang_tshuox__xiaojiu"] = "可不能打女孩子哦~",
  ["~tshuox__xiaojiu"] = "嗯哼..你欺负人家..",
}


local xiaotao = General(extension, "tshuox__xiaotao","god",4,4,General.Female)
xiaotao.hidden = true
local taoyan = fk.CreateTriggerSkill{
  name = "tshu__taoyan",
  anim_type = "drawCards",
  events = {fk.TurnStart},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) 
  end,
  on_cost = function(self, event, target, player, data)
    local targets = {}
    local room = player.room
    for _, p in ipairs(room.alive_players) do
      if p.role == "lord" or p.role == "loyalist" then
        table.insertIfNeed(targets,p.id)
      end
    end
    local tar = room:askForChoosePlayers(player,targets,1,2,"#tshu__taoyan",self.name,true)
    if #tar > 0 then
      self.cost_data = tar
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for _, pid in ipairs(self.cost_data) do
      local p = room:getPlayerById(pid)
      if not p.dead then
        local ids = room:getCardsFromPileByRule("peach")
        if #ids > 0 then
          room:obtainCard(p, ids[1], true, fk.ReasonPrey)
        end
      end
      if not p.dead then
        p:drawCards(1, self.name)
      end
    end
  end,
}
local yanli = fk.CreateTriggerSkill{
  name = "tshu__yanli",
  anim_type = "drawCards",
  events = {fk.EnterDying},
  can_trigger = function(self, event, target, player, data)
    return  player:hasSkill(self) and (target.role == "lord" or target.role == "loyalist" or target == player) 
    and player:usedSkillTimes(self.name, Player.HistoryRound) < 1 and player.phase == Player.NotActive
  end,
  on_cost = function(self, event, target, player, data)
    if player.room:askForSkillInvoke(player,self.name,nil) then
      player.room:doIndicate(player.id, {target.id})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if target.maxHp > 0 then
      room:recover({
      who = target,
      num = 1 - target.hp,
      recoverBy = player,
      skillName = self.name
      })
    end
    if not target.dead then
      target:drawCards(1,self.name)
    end
  end,
  
}
xiaotao:addSkill(taoyan)
xiaotao:addSkill(yanli)
xiaotao:addSkill("tshu__xuxiang")
xiaotao:addSkill("keji")

Fk:loadTranslationTable{
  ["tshuox__xiaotao"] = "小桃",

  ["tshu__taoyan"] = "桃宴",
  [":tshu__taoyan"] = "回合开始时，你可以令至多两名其他角色从牌堆获得一张【桃】并摸一张牌。",
  ["tshu__yanli"] = "妍丽",
  [":tshu__yanli"] = " 你的回合外，当一名角色进入濒死状态时，你可以令其回复至1点体力并摸一张牌(每轮限一次)。",
  ["#tshu__taoyan"] = "你可以让至多俩名主忠从牌堆中各获得一张【桃】并摸一张牌",

  ["$tshu__taoyan"] = "灿若桃花，琼华宴晚。",
  ["$tshu__yanli"] = "妍姿秀丽，天然如花。",
  ["$tshu__xuxiang_tshuox__xiaotao"] = "可不能打女孩子哦~",
  ["~tshuox__xiaotao"] = "嗯哼..你欺负人家..",
}

local xiaole = General(extension, "tshuox__xiaole","god",4,4,General.Female)
xiaole.hidden = true
local leyu = fk.CreateTriggerSkill{
  name = "tshu__leyu",
  anim_type = "control",
  events = {fk.TurnStart},
  can_trigger = function(self, event, target, player, data)
    return  player:hasSkill(self) and target.role == "rebel" and not player:isNude()
  end,
  on_cost = function(self, event, target, player, data)
    -- 计算玩家手牌数和装备数总和
    local card_count = #player:getCardIds(Player.Hand) + #player:getCardIds(Player.Equip)
    if card_count < 3 then return false end  -- 如果牌不够，直接返回false
    
    -- 强制弃置三张牌，不给取消的机会
    local forced_discard = true
    local cards = player.room:askForDiscard(player, 3, 3, true, self.name, forced_discard, ".", "#tshu__leyu-invoke::"..target.id, false)
    
    -- 如果没有选择足够的牌，但玩家有足够的牌，则强制随机弃置
    if #cards < 3 and card_count >= 3 then
      local all_cards = {}
      for _, area in ipairs({Player.Hand, Player.Equip}) do
        for _, id in ipairs(player:getCardIds(area)) do
          table.insert(all_cards, id)
        end
      end
      
      -- 随机选择3张牌
      local to_discard = {}
      while #to_discard < 3 and #all_cards > 0 do
        local random_index = math.random(1, #all_cards)
        table.insert(to_discard, all_cards[random_index])
        table.remove(all_cards, random_index)
      end
      
      cards = to_discard
    end
    
    if #cards > 2 then
      self.cost_data = cards
      return true
    end
    
    return false
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player.room:throwCard(self.cost_data, self.name, player, player)
    
    local judge = {
      who = target,
      reason = "indulgence",
      pattern = ".|.|spade,club,diamond",
    }
    room:judge(judge)
    local result = judge.card
    if result.suit ~= Card.Heart then
      target:skip(Player.Play)
    end
  end,
}
local yuanli = fk.CreateTriggerSkill{
  name = "tshu__yuanli",
  anim_type = "drawcard",
  events = {fk.EventPhaseSkipping},
  can_trigger = function(self, event, target, player, data)
    return  player:hasSkill(self) and target.skipped_phases[Player.Play]  and true--FIXME: 此时机无data，需补充
      
  end,
  on_cost = function(self, event, target, player, data)
    local targets = {}
    local room = player.room
    for _, p in ipairs(room.alive_players) do
      if p.role == "lord" or p.role == "loyalist" then
        table.insertIfNeed(targets,p.id)
      end
    end
    local tar = room:askForChoosePlayers(player,targets,1,1,nil,self.name,false)
    if #tar > 0 then
      self.cost_data = tar
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    if not player.dead then
      player:drawCards(1, self.name)
    end
    local to = player.room:getPlayerById(self.cost_data[1])
    if not to.dead then
      to:drawCards(1,self.name)
    end
  end,
}
xiaole:addSkill(leyu)
xiaole:addSkill(yuanli)
xiaole:addSkill("tshu__xuxiang")
xiaole:addSkill("keji")

Fk:loadTranslationTable{
  ["tshuox__xiaole"] = "小乐",

  ["tshu__leyu"] = "乐虞",
  [":tshu__leyu"] = " 一名角色的回合开始时，你可以弃置三张牌令其判定，若结果不为红桃，则其跳过出牌阶段。",
  ["tshu__yuanli"] = "媛丽",
  [":tshu__yuanli"] = " 当一名角色跳过出牌阶段时，你可以选择一名其他角色，你与其各摸一张牌。",
  ["#tshu__leyu-invoke"] = "你可以弃置3张牌令%dest跳过本回合下次出牌阶段",

  ["$tshu__leyu"] = "虞歌爱晚，此间为乐。",
  ["$tshu__yuanli"] = "媛媛相协，心有灵犀。",
  ["$tshu__xuxiang_tshuox__xiaole"] = "可不能打女孩子哦~",
  ["~tshuox__xiaole"] = "嗯哼..你欺负人家..",
}

local xiaoshan = General(extension, "tshuox__xiaoshan","god",4,4,General.Female)
xiaoshan.hidden = true
local shanwu = fk.CreateTriggerSkill{
  name = "tshu__shanwu",
  anim_type = "support",
  frequency = Skill.Compulsory,
  events = {fk.TargetSpecifying},
  can_trigger = function(self, event, target, player, data)
    local room = player.room
    if data.card.trueName == "slash" then
      for _, pid in ipairs(AimGroup:getAllTargets(data.tos)) do
        local p = room:getPlayerById(pid)
        if (p.role == "lord" or p.role == "loyalist" ) then
          -- 检查玩家是否有闪
          local has_jink = false
          for _, id in ipairs(player:getCardIds(Player.Hand)) do
            if Fk:getCardById(id).name == "jink" then
              has_jink = true
              break
            end
          end
          return player:hasSkill(self) and has_jink
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    -- 寻找一张闪
    local jink_id = nil
    for _, id in ipairs(player:getCardIds(Player.Hand)) do
      if Fk:getCardById(id).name == "jink" then
        jink_id = id
        break
      end
    end
    
    if jink_id then
      self.cost_data = {jink_id}
      return true
    end
    return false
  end,
  on_use = function(self, event, target, player, data)
    player.room:throwCard(self.cost_data, self.name, player, player)
    player.room:doIndicate(player.id, {target.id})
    for _, pid in ipairs(AimGroup:getAllTargets(data.tos)) do
      AimGroup:cancelTarget(data, pid)
    end
    player.room:sendLog{ type = "#CardNullifiedBySkill", from = target.id, arg = self.name, arg2 = data.card:toLogString() }
  end,
}
local xianli = fk.CreateTriggerSkill{
  name = "tshu__xianli",
  mute = true,
  events = {fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if player.dead then return false end
    if player.room.current.dead then return false end
    local card
    for _, move in ipairs(data) do
      if move.from == player.id then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerHand  then
            card = Fk:getCardById(info.cardId)
            if card.name == "jink" then
              return player:hasSkill(self) and (player.room.current.role ~= "lord" or player.room.current.role ~= "loyalist") and player:usedSkillTimes(self.name, Player.HistoryTurn) < 2 and not player.room.current:isNude()
            end
          end
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local card = room:askForCardChosen(player,player.room.current, "he", self.name)
    room:obtainCard(player, card, false, fk.ReasonPrey)
  end,
}
xiaoshan:addSkill(shanwu)
xiaoshan:addSkill(xianli)
xiaoshan:addSkill("tshu__xuxiang")
xiaoshan:addSkill("keji")

Fk:loadTranslationTable{
  ["tshuox__xiaoshan"] = "小闪",

  ["tshu__shanwu"] = "闪舞",
  [":tshu__shanwu"] = "锁定技，当其他角色成为【杀】的目标时，若你有【闪】，你弃置一张【闪】取消该【杀】的所有目标",
  ["tshu__xianli"] = "娴丽",
  [":tshu__xianli"] = "每回合限两次，当你失去手牌中的【闪】时，你可以获得当前回合角色的一张牌。",
  ["#tshu__shanwu-invoke"] = "你可以弃置一张【闪】取消此【杀】的所有目标",

  ["$tshu__shanwu"] = "见风起舞，舞步轻盈",
  ["$tshu__xianli"] = "娴丽曼姚，宜仕怡家",
  ["$tshu__xuxiang_tshuox__xiaoshan"] = "可不能打女孩子哦~",
  ["~tshuox__xiaoshan"] = "嗯哼..你欺负人家..",
}
return extension